package com.mianyang.zhangsan.common.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;

public class RequestHelper {

    public static String currentAccountName() {
        HttpServletRequest request = getRequest();
        if (request != null) {
            Object accountName = request.getAttribute("accountName");
            return accountName != null ? accountName.toString() : "";
        }
        return "";
    }

    public static Long currentAccountId() {
        HttpServletRequest request = getRequest();
        if (request != null) {
            Object accountId = request.getAttribute("accountId");
            return accountId != null ? Long.parseLong(accountId.toString()) : null;
        }
        return null;
    }

    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            return (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        }
        return null;
    }
}
